package com.unjuanable.safetymonitor.domain.controlmng;

import com.unjuanable.safetymonitor.application.controlmng.ControlDto;
import com.unjuanable.safetymonitor.domain.common.validator.DateTimeValidator;
import com.unjuanable.safetymonitor.domain.common.validator.InputTextValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @description
 */
@Component
public class ControlBuilder {

    private final InputTextValidator inputTextValidator;
    private final DateTimeValidator dateTimeValidator;

    private Long userId;
    private String name;
    private String target;
    private LocalDateTime startAt;
    private LocalDateTime endAt;
    private Long createdBy;

    @Autowired
    public ControlBuilder(InputTextValidator inputTextValidator, DateTimeValidator dateTimeValidator) {
        this.inputTextValidator = inputTextValidator;
        this.dateTimeValidator = dateTimeValidator;
    }

    public ControlBuilder userId(Long userId) {
        this.userId = userId;
        return this;
    }

    public ControlBuilder name(String name) {
        this.name = name;
        return this;
    }

    public ControlBuilder target(String target) {
        this.target = target;
        return this;
    }

    public ControlBuilder startAt(LocalDateTime startAt) {
        this.startAt = startAt;
        return this;
    }

    public ControlBuilder endAt(LocalDateTime endAt) {
        this.endAt = endAt;
        return this;
    }

    public ControlBuilder createdBy(Long createdBy) {
        this.createdBy = createdBy;
        return this;
    }

    public ControlTask build() {
        validate();

        ControlTask controlTask = new ControlTask();
        controlTask.setUserId(this.userId);
        controlTask.setName(this.name);
        controlTask.setTarget(this.target);
        controlTask.setStartAt(this.startAt);
        controlTask.setEndAt(this.endAt);
        controlTask.setCreatedBy(this.createdBy);
        controlTask.setCreatedAt(LocalDateTime.now());

        return controlTask;
    }

    private void validate() {
        inputTextValidator.defaultInputShouldNotLongerThan(this.name);
        dateTimeValidator.startTimeShouldEarly(this.startAt, this.endAt);
    }
}
